package com.leinao.core.pool;

import com.leinao.domain.TableInfo;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 表信息池
 *
 * @author zhong.wang
 * @date 2025/1/15
 */
public class TableInfoPool {
    private static final TableInfoPool INSTANCE = new TableInfoPool();

    /**
     * 设备模型id pool
     */
    private List<Long> deviceModelIdPool;

    /**
     * 表信息 pool
     */
    private Map<Long, List<TableInfo>> tableInfoPool;

    /**
     * 表信息 pool
     */
    private List<TableInfo> tableInfos;

    private TableInfoPool(){
        deviceModelIdPool = new CopyOnWriteArrayList<>();
        tableInfoPool = new ConcurrentHashMap<>(1<<10);
        tableInfos = new CopyOnWriteArrayList<>();
    }

    /**
     * getInstance
     * @return
     */
    public static TableInfoPool getInstance() {
        return INSTANCE;
    }

    /**
     * 上线 设备模型
     * @param deviceModelId
     */
    public synchronized void registerDeviceModelId(Long deviceModelId){
        this.deviceModelIdPool.add(deviceModelId);
    }

    /**
     * 上线 表信息
     * @param deviceModelId
     */
    public synchronized void registerTableInfo(Long deviceModelId,List<TableInfo> tableInfos){
        this.tableInfos.addAll(tableInfos);
        this.tableInfoPool.putIfAbsent(deviceModelId,tableInfos);
    }

    /**
     * 获取表信息
     * @return
     */
    public List<TableInfo> getTableInfos(){
        return tableInfos;
    }

    public Map<Long, List<TableInfo>> getTableInfoPool(){
        return tableInfoPool;
    }

    public List<Long> getDeviceModelIdPool(){
        return deviceModelIdPool;
    }
}
